package com.wy.threeballsloading;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import androidx.annotation.Nullable;

/**
 * Time:2021/9/15
 * Author:ypf
 * Description:
 */
public class ThreeBallsLoadingDrawable extends View {

    private String TAG = "ThreeBallsLoadingDrawable";

    private Paint mBall1Paint;
    private Paint mBall2Paint;
    private Paint mBall3Paint;

    private float translateX;

    public ThreeBallsLoadingDrawable(Context context) {
        this(context, null, 0);
    }

    public ThreeBallsLoadingDrawable(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ThreeBallsLoadingDrawable(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        mBall1Paint = new Paint();
        mBall1Paint.setColor(Color.RED);
        mBall1Paint.setAntiAlias(true);
        mBall1Paint.setDither(true);

        mBall2Paint = new Paint();
        mBall2Paint.setColor(Color.BLUE);
        mBall2Paint.setAntiAlias(true);
        mBall2Paint.setDither(true);

        mBall3Paint = new Paint();
        mBall3Paint.setColor(Color.GREEN);
        mBall3Paint.setAntiAlias(true);
        mBall3Paint.setDither(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        canvas.drawCircle(width / 2, height / 2, 20, mBall1Paint);
        canvas.drawCircle(width / 2 - 20 - 80 + translateX, height / 2, 20, mBall2Paint);
        canvas.drawCircle(width / 2 + 20 + 80 - translateX, height / 2, 20, mBall3Paint);
    }

    public void innerMove() {

        ValueAnimator translateXInnerAnimation = ValueAnimator.ofFloat(0, 100);
        translateXInnerAnimation.setDuration(500);
        translateXInnerAnimation.setInterpolator(new AccelerateInterpolator());
        translateXInnerAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                translateX = (float) translateXInnerAnimation.getAnimatedValue();
                Log.i(TAG, "translateX:" + translateX);
                invalidate();
            }
        });

        translateXInnerAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                exchangeColor();
                expendMove();
            }
        });

        translateXInnerAnimation.start();

    }

    public void expendMove() {

        ValueAnimator translateXInnerAnimation = ValueAnimator.ofFloat(100, 0);
        translateXInnerAnimation.setDuration(500);
        translateXInnerAnimation.setInterpolator(new AccelerateInterpolator());
        translateXInnerAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                translateX = (float) translateXInnerAnimation.getAnimatedValue();
                Log.i(TAG, "translateX:" + translateX);
                invalidate();
            }
        });

        translateXInnerAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                exchangeColor();
                innerMove();
            }
        });

        translateXInnerAnimation.start();

    }

    /**
     * 变换颜色方法
     */
    private void exchangeColor() {

        int mLeftPaintColor = mBall1Paint.getColor();
        int mRightPaintColor = mBall2Paint.getColor();
        int mMiddlePaintColor = mBall3Paint.getColor();
        mBall1Paint.setColor(mRightPaintColor);
        mBall2Paint.setColor(mMiddlePaintColor);
        mBall3Paint.setColor(mLeftPaintColor);
        invalidate();
    }

}
